package com.zretc.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.entity.Permission;
import com.zretc.entity.ResponseResult;
import com.zretc.entity.Role;
import com.zretc.entity.UserInfo;
import com.zretc.service.RoleService;
import com.zretc.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author 周文涛
 * 2020年8月19日20:29:20
 * 用户控制层
 */
@RestController
@RequestMapping("user/info")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/")
    public List<UserInfo> getAllUser(String keyword){
        HashMap<String, String> condition = new HashMap<>();
        condition.put("keyword",keyword);
        return userInfoService.getAllUser(condition);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public ResponseResult deleteUser(@PathVariable("id")Integer userId){
        return userInfoService.deleteUser(userId);
    }
    // 更新用户角色
    @PutMapping("/")
    public ResponseResult updateUserRole(@RequestParam("userId")Integer userId,
                                         @RequestParam("roleIds")Integer[] roleIds){
        return roleService.updateUserRole(userId,roleIds);
    }

    // 是否禁用用户
    @PutMapping("/enabled")
    public ResponseResult enabledUser(@RequestBody UserInfo userInfo){
        return userInfoService.updateEnabled(userInfo);
    }

    // 全部角色
    @GetMapping("/role")
    public List<Role> getAllRoles(){
        return roleService.finAll();
    }

}
